package com.my.yc.d0316.biz;

import com.my.yc.util.DBHelper;

import java.sql.Date;

public class BookBiz {

    public void check(String bookname, String bookauthor,
                      String bookpress, String pressdate,
                      String bookimage) throws BizException {
        if (bookname == null || bookname.trim().isEmpty()){
            throw new BizException("请输入书名");
        }
        if(bookpress.length() < 3){
            throw new BizException("出版社至少是3个字符");
        }
        // mysql默认日期格式 yyyy-mm-dd
        if (pressdate.matches("\\d{4}-\\d{1,2}-\\d{1,2}") == false){
            throw new BizException("出版日期格式不正确");
        }
    }

    public void create(String bookname, String bookauthor,
                       String bookpress, String pressdate,
                       String bookimage) throws BizException {

        check( bookname, bookauthor,
                 bookpress, pressdate,
                 bookimage);
        String sql = "insert into books (bookname,bookauthor,bookpress,pressdate,bookimage)" +
                " values (?,?,?,?,?)";

        DBHelper.update(sql,bookname,bookauthor,bookpress, Date.valueOf(pressdate), bookimage);
    }
    public void modify( String id,String bookname, String bookauthor,
                       String bookpress, String pressdate,
                       String bookimage) throws BizException {
        check( bookname, bookauthor,
                bookpress, pressdate,
                bookimage);

        String sql = "update books set bookname = ?,bookauthor = ?,bookpress = ?,pressdate = ?,bookimage = ?" +
                " where id = ?";

        DBHelper.update(sql,bookname,bookauthor,bookpress, Date.valueOf(pressdate), bookimage,id);
    }

    public void remove(String id){
        String sql = "delete from books where id = ?";
        DBHelper.update(sql,id);
    }
    public void remove1(String id){
        String sql = "update books set status = 0 where id = ?";
        DBHelper.update(sql,id);
    }

}
